import requests
from bs4 import BeautifulSoup
from check_ip import check_proxy_ip
import time
import random


headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
}

for page in range(1, 8):
    url = f"http://www.ip3366.net/?stype=1&page={page}"
    response = requests.get(url, headers=headers)
    response.encoding = response.apparent_encoding

    bs = BeautifulSoup(response.text, "lxml")
    """
    
    #list > table > tbody > tr
    td:nth-child(1)
    
    """
    tr_list = bs.select("#list > table > tbody > tr")
    for tr in tr_list:
        ip = tr.select_one("td:nth-child(1)").text
        port = tr.select_one("td:nth-child(2)").text

        rv = check_proxy_ip(ip, port)
        if rv:
            print(ip, port, " 有效!")

    t = round(random.random(), 3)
    time.sleep(t)

